<?php
class WeChat{
	//private $ACCESS_TOKEN = 'KUhH07KTRTH_1IjbGDku2JYeQBtnlY8au53-voodnk8_5JoNvjJUCkF7S7ctwREZBSIPg-EbTYng_OIS1cjXArKZlAqegqaAsjS6YwD_RES8nLvAydnxIDTKSEnRNaRKTIChACAODX' ;
	//private $AppId = 'wxe91516974138c2c1';
	//private $AppSecret = 'c241a450e9997536cab6396bca392c87';
	private $Token = '' ;
	private $AppId = '';
	private $AppSecret = '' ;
    /**
     * 获取所有关注公众号人openid
     * @return [type] [description]
     */
	public function getAllOpenid(){
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN";
        $data = array('ACCESS_TOKEN'=>$this->Token);
        $url = $this -> replaceUrl($url,$data);
        return $this->getCurl($url);
    }
    /**
     * 根据openid获取用户信息
     * @param  String $openid openid
     * @return [type]         [description]
     */
    public function getUserInfoByOpenId($openid){
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN ';
        $data = array('ACCESS_TOKEN'=>$this->Token,'OPENID'=>$openid);
        $url = $this -> replaceUrl($url,$data);
        return $this->getCurl($url);
    }
    /**
     * 获取token
     * @return [type] [description]
     */
    public function gettoken(){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        $data = array('APPID'=>$this->AppId,'APPSECRET'=>$this->AppSecret);
        $url = $this -> replaceUrl($url,$data);
        $data = $this->getCurl($url);
        return $data['access_token'];
        
    }
    /*
     * 自定义菜单
     */
    public function createview($view){
    	$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    	$data = array('ACCESS_TOKEN'=>$this->Token);
    	$url = $this -> replaceUrl($url,$data);	
    	return $this->postCurl($url,$view);
    }
    /**
     * 网页授权1
     * @param int $type   1 静默授权   2 网页授权
     * @param string $redirect_uri  回调地址
     */
    public function authorize1($type,$redirect_uri){
    	  switch($type){
    	 	case 1:$scope = 'snsapi_base';break;   ///静默授权  
    	 	case 2:$scope = 'snsapi_userinfo';break;///网页授权
    	 }
		 
    	$data = array('APPID'=>$this->AppId,'REDIRECT_URI'=>urlencode($redirect_uri),'SCOPE'=>$scope);
    	$url = "http://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=1#wechat_redirect";
    	$url = $this -> replaceUrl($url,$data);
		return $url;
    }
    /**
     * 网页授权2
     * @param string $code 由回调地址得到的code
     * @return mixed
     */
    public function authorize2($code){
    	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code ";
    	$data = array('APPID'=>$this->AppId,'SECRET'=>$this->AppSecret,'CODE'=>$code);
    	$url = $this -> replaceUrl($url,$data);
    	return  $this -> getCurl($url);
    }
    /**
     * 网页jdk开发
     * @return mixed
     */
	public function getjsapi(){
    	$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
    	$data = array('ACCESS_TOKEN'=>$this->Token);
    	$url = $this -> replaceUrl($url,$data);
    	return  $this -> getCurl($url);
    }
	////下载临时素材
	public function download($media_id){
	   $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	   $data = array('ACCESS_TOKEN'=>$this->Token,'MEDIA_ID'=>$media_id);
	   $url = $this -> replaceUrl($url,$data);
       return $this -> getCurl($url,false);
	}
	/**
	 * 新增临时信息
	 * @param string $content  json格式
	 * @return mixed
	 */
	public function addnews($content){
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
		$data = array('ACCESS_TOKEN'=>$this->Token);
		$url = $this -> replaceUrl($url,$data);
		return $this -> postCurl($url,$content);
	}
	/**
	**获取模板行业信息
	*/
	public function getmp(){
	   $url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";
	   $data = array('ACCESS_TOKEN'=>$this->Token);
	   $url = $this -> replaceUrl($url,$data);
       return $this -> getCurl($url);
	}
	/**
	 * 获取模板列表
	 * @return mixed
	 */
	public function templateList(){
		$url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
		$data = array('ACCESS_TOKEN'=>$this->Token);
		$url = $this -> replaceUrl($url,$data);
		return $this -> getCurl($url);
	}
	public function sendTemplate($json){
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
	    $data = array('ACCESS_TOKEN'=>$this->Token);
	    $url = $this -> replaceUrl($url,$data);
	    return $this -> postCurl($url,$json);
	}
    /**
     * 拼装完整url接口地址
     * @param  String $url 接口地址
     * @param  array $data 必要信息
     * @return String $url 完整接口地址
     */
    private function replaceUrl($url,$data){
       foreach ($data as $key => $value) {
           $url = str_replace($key,$value,$url);
       }
       return $url;
    }
    private function getCurl($url,$isjson=true){
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$url);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	//3、连接并得到结果
    	$rs=curl_exec($ch);
    	//4、关闭cURL资源，并且释放系统资源
    	curl_close($ch);
		if($isjson){
			return json_decode($rs,true);
		}
    	return $rs;
    }
    
    private function postCurl($url,$data=''){
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$url);
    	curl_setopt($ch,CURLOPT_POST,true);
    	if($data){
    		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    	}
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    	//3、连接并得到结果
    	$rs=curl_exec($ch);
    	//4、关闭cURL资源，并且释放系统资源
    	curl_close($ch);
    	return json_decode($rs,true);
    }
    
    public function setToken($token){
        $this ->Token = $token;
    } 
    public function setAppId($AppId){
        $this ->AppId = $AppId;
    } 
    public function setAppSecret($AppSecret){
        $this ->AppSecret = $AppSecret;
    } 
} 
?>